﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormDesigner.MyControls
{
    public partial class MyControl : UserControl
    {
        public static PropertyGrid PropertyGrid { get; } = new PropertyGrid() { Dock = DockStyle.Fill };
        public static int MinSize { set; get; } = 60;

        private readonly List<Button> buttons;
        public List<Button> Buttons
        {
            get
            {
                return buttons;
            }
        }

        public MyControl()
        {
            InitializeComponent();
            buttons =
            [
                button_LT,
                button_LM,
                button_LB,
                button_RT,
                button_RM,
                button_RB,
                button_MT,
                button_MB
            ];
            Load += MyControl_Load;
            Enter += MyControl_Enter;
            Leave += MyControl_Leave;
        }

        private void MyControl_Load(object? sender, EventArgs e)
        {
            foreach (Button button in buttons)
            {
                button.MouseDown += Button_MouseDown;
                button.MouseUp += Button_MouseUp;

                button.Visible = false;
            }
        }

        private void MyControl_Enter(object? sender, EventArgs e)
        {
            PropertyGrid.SelectedObject = sender;

            foreach (Button button in buttons)
            {
                button.Visible = true;
            }
        }

        private void MyControl_Leave(object? sender, EventArgs e)
        {
            foreach (Button button in buttons)
            {
                button.Visible = false;
            }
        }

        Button? button;
        Point start;
        Point end;

        private void Button_MouseUp(object? sender, MouseEventArgs e)
        {
            if (button != null)
            {
                end = MousePosition;

                int offsetX = end.X - start.X;
                int offsetY = end.Y - start.Y;

                if (e.Button == MouseButtons.Left)
                {

                    if (button.Name.Contains('R'))
                    {
                        Width += offsetX;
                        Width = Math.Min(Width, end.X);
                        Width = Math.Max(Width, MinSize);
                    }
                    else if (button.Name.Contains('L'))
                    {
                        Width -= offsetX;
                        Left += offsetX;
                        Width = Math.Max(Width, MinSize);
                    }

                    if (button.Name.Contains('B'))
                    {
                        Height += offsetY;
                        Height = Math.Min(Height, end.Y);
                        Height = Math.Max(Height, MinSize);
                    }
                    else if (button.Name.Contains('T'))
                    {
                        Height -= offsetY;
                        Top += offsetY;
                        Height = Math.Max(Height, MinSize);
                    }
                }
                else if (e.Button == MouseButtons.Middle)
                {
                    Left += offsetX;
                    Top += offsetY;
                }
            }
            button = null;
        }

        private void Button_MouseDown(object? sender, MouseEventArgs e)
        {
            button = (sender as Button);
            start = MousePosition;
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            button_LT.Location = new Point(0, 0);
            button_LM.Location = new Point(0, Height / 2 - button_LM.Height / 2);
            button_LB.Location = new Point(0, Height - button_LB.Height);

            button_MT.Location = new Point(Width / 2 - button_MT.Width / 2, 0);
            button_MB.Location = new Point(Width / 2 - button_MB.Width / 2, Height - button_LB.Height);

            button_RT.Location = new Point(Width - button_RT.Width, 0);
            button_RM.Location = new Point(Width - button_RB.Width, Height / 2 - button_LM.Height / 2);
            button_RB.Location = new Point(Width - button_RB.Width, Height - button_LB.Height);

            PropertyGrid.Refresh();
        }

        protected override void OnMove(EventArgs e)
        {
            base.OnMove(e);
            PropertyGrid.Refresh();
        }

        private void DeleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PropertyGrid.SelectedObject = null;
            Parent?.Controls.Remove(this);
        }
    }
}
